#include "Avalon/Codecs/CodedWriter.hpp"
#include "Avalon/Codecs/Encoder.hpp"
#include "Avalon/IO/Buffer.hpp"

using namespace Avalon;
using namespace Avalon::Codecs;
using namespace Avalon::IO;
using namespace Avalon::Threading;

CodedWriter::CodedWriter(Carry<Writer> destination, Encoder* encoder)
    : m_destination(destination),
      m_encoder(encoder) {}

CodedWriter::~CodedWriter() {}

void CodedWriter::Write(const void* data, int size, Out<Async<void> > result) {
  Buffer encodedData;
  int encodedSize;
  m_encoder->Encode(data, size, &encodedData, Store(encodedSize));
  m_destination->Write(encodedData, Store(result));
}

void CodedWriter::Write(const Buffer& data, Out<Async<void> > result) {
  Buffer encodedData;
  int encodedSize;
  m_encoder->Encode(data, &encodedData, Store(encodedSize));
  m_destination->Write(encodedData, Store(result));
}
